package com.youmingxing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.selfInfo.PersonalInfoActivity;
import com.youmingxing.bean.selfinfo.SearchWork;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class SearchMoreWorkActivity extends BaseActivity {
    RefreshListView searchListView;
    List<SearchWork> mSearchWorks = new ArrayList<>();
    int mark = 1;
    private String keyword;
    private SearchMoreWorkAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_search_more_user);
        setTopbarName("更多作品");
        keyword = getIntent().getStringExtra("keyword");
        searchListView = (RefreshListView) findViewById(R.id.search_list);
        mark = 1;
        mSearchWorks.clear();
        AllStarApi.searchMoreWork(keyword, mark, initHandler);

        searchListView.setOnLoadMoreListener(new RefreshListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore()
            {
                AllStarApi.searchMoreWork(keyword, mark, moreHandler);
            }
        });

        searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (id < 0) {
                    return;
                }
                int realPosition = (int) id;
                Intent intent = new Intent(SearchMoreWorkActivity.this, ContentDetailActivity.class);
                intent.putExtra("content_id", mSearchWorks.get(realPosition).getId());
                startActivity(intent);
            }
        });

    }

    BaseJsonHandler initHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            JSONArray data = response.optJSONArray("data");
            adapter = new SearchMoreWorkAdapter();
            if (data.length() == 0 && mark == 1) {
                searchListView.noData("抱歉,没有您要的搜索结果");
            } else {
                searchListView.setFootVisible(false);
                for (int i = 0; i < data.length(); i++) {
                    JSONObject jsonObject = data.optJSONObject(i);
                    SearchWork searchWork = new SearchWork();
                    searchWork.setImg(jsonObject.optString("img"));
                    searchWork.setId(jsonObject.optInt("id"));
                    searchWork.setTitle(jsonObject.optString("title"));
                    searchWork.setType(jsonObject.optInt("type"));
                    searchWork.setTip(jsonObject.optString("tip"));
                    mSearchWorks.add(searchWork);
                }
                if (data.length() < 20) {
                    searchListView.setFootVisible(true);
                    searchListView.noMoreData();
                }
                adapter.notifyDataSetChanged();
                mark++;
            }
            searchListView.setAdapter(adapter);
        }
    };

    BaseJsonHandler moreHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            searchListView.endLoadMore();
            mSearchWorks.clear();
            searchListView.setFootVisible(true);
            JSONArray data = response.optJSONArray("data");
            if (data.length() == 0) {
                if (mark == 1) {
                    searchListView.noData("抱歉,没有您要的搜索结果");
                } else {
                    searchListView.noMoreData();
                }
            } else {
                for (int i = 0; i < data.length(); i++) {
                    JSONObject jsonObject = data.optJSONObject(i);
                    SearchWork searchWork = new SearchWork();
                    searchWork.setImg(jsonObject.optString("img"));
                    searchWork.setId(jsonObject.optInt("id"));
                    searchWork.setTitle(jsonObject.optString("title"));
                    searchWork.setType(jsonObject.optInt("type"));
                    searchWork.setTip(jsonObject.optString("tip"));
                    mSearchWorks.add(searchWork);
                }
                adapter.notifyDataSetChanged();
                mark++;
            }
        }
    };

    private class ViewHolder {
        ImageView workImage;
        TextView workName;
        TextView workInfo;
    }

    private class SearchMoreWorkAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mSearchWorks.size();
        }

        @Override
        public Object getItem(int position) {
            return mSearchWorks.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public android.view.View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(SearchMoreWorkActivity.this, R.layout.list_item_search, null);
                holder = new ViewHolder();
                holder.workImage = (ImageView) convertView.findViewById(R.id.iv_image);
                holder.workName = (TextView) convertView.findViewById(R.id.tv_name);
                holder.workInfo = (TextView) convertView.findViewById(R.id.tv_info);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            SearchWork searchWork = mSearchWorks.get(position);
            Glide.with(SearchMoreWorkActivity.this).load(searchWork.getImg()).placeholder(R
                    .drawable.default_16_9).animate(R.anim.abc_fade_in).into(holder.workImage);
            holder.workName.setText(searchWork.getTitle());
            holder.workInfo.setText(searchWork.getTip());
            return convertView;
        }

    }
}
